package org.jetnuke.modules.pictures;

import org.apache.log4j.Logger;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.util.preferences.ConfigKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.PictureAlbumDAO;
import org.jetnuke.dao.PictureCommentDAO;
import org.jetnuke.dao.PictureDAO;
import org.jetnuke.dao.PictureVoteDAO;
import org.jetnuke.entities.Picture;
import org.jetnuke.entities.PictureAlbum;
import org.jetnuke.modules.pictures.utils.PictureUtils;
import org.jetnuke.util.JetUtil;

public class RemoveAlbum implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(RemoveAlbum.class);

	int aid;
	String ok;

	public void setOk(String ok) {
		this.ok = ok;
	}

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		if (ui == null || ui.getID() < 2)
			return null;
		if (ok == null) {
			PictureUtils.manageStyleAndBlocks(vc, ui.getID());
			vc.put("myPictures", Boolean.TRUE);
			ActionForward returnActionForward = new ActionForward(op, info);
			if (logger.isDebugEnabled()) {
				logger
						.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
			}
			return returnActionForward;
		}
		PictureAlbumDAO pad = DataAccessDriver.getInst().newPictureAlbumDAO();
		PictureAlbum album = pad.retrieveByPK(aid);
		if (album.getUID() != ui.getID())
			return null;
		PictureDAO pd = DataAccessDriver.getInst().newPictureDAO();
		List pictures = pd.selectByAlbum(aid);
		for (int i = 0; i < pictures.size(); i++) {
			Picture picture = (Picture) pictures.get(i);

			String initPath = SystemGlobals
					.getValue(ConfigKeys.ATTACHMENTS_STORE_DIR)
					+ "/pictures/" + ui.getID() + "/";
			File f = null;
			File pathDir = new File(initPath);
			f = new File(pathDir, picture.getFILENAME());
			if (f.exists())
				f.delete();
			if (picture.getMEDIUMNAME() != null) {
				f = new File(pathDir, picture.getMEDIUMNAME());
				if (f.exists())
					f.delete();
			}
			if (picture.getTHUMBNAME() != null) {
				f = new File(pathDir, picture.getTHUMBNAME());
				if (f.exists())
					f.delete();
			}

			PictureCommentDAO pcd = DataAccessDriver.getInst()
					.newPictureCommentDAO();
			pcd.deleteByPID(picture.getPID());
			PictureVoteDAO pvd = DataAccessDriver.getInst().newPictureVoteDAO();
			pvd.deleteByPID(picture.getPID());
			pd.deleteByPID(picture.getPID());
		}
		pad.deleteByAID(aid);
		JetUtil.redirect(vc, response, request.getContextPath()
				+ "/do/pictures/");

		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return null;
	}

	public void setAid(int aid) {
		this.aid = aid;
	}

}
